import { commonApi } from '@/common/api/commonApi.js'
import _ from 'lodash'

export const orgGridTree = {
  buildTree: async (type = 'admin', organizationId) => {
    let videos = []
    if (type === 'admin') {
      videos = await commonApi.search('measure_video', {exists: 'orgId', limit: 1000, include: ['gridId', 'orgId']})
    } else {
      videos = await commonApi.search('measure_video', {
        equalTo: {orgId: organizationId},
        limit: 1000,
        include: ['gridId', 'orgId']
      })
    }
    let orgIds = _.groupBy(videos, function (item) {
      return item.orgId.attributes.Name
    })
    let orgGridsArr = []
    for (let key in orgIds) {
      let children = _.groupBy(orgIds[key], function (item) {
        return item.gridId.attributes.name
      })
      let childrenArr = []
      for (let k2 in children) {
        let obj2 = {
          label: k2,
          value: children[k2]
        }
        childrenArr.push(obj2)
      }
      let obj = {
        label: key,
        children: childrenArr
      }
      orgGridsArr.push(obj)
    }

    orgGridsArr = orgGridsArr.filter(v => v.label !== '安监局')
    return orgGridsArr
  }
}
